/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package databook.util;

import java.awt.Color;

import prefuse.util.ColorLib;

/**
 *
 * @author zach
 */
public class SimpleIntColorTransformFunction implements TransformFunction {
    private int START_COLOR;
    private int END_COLOR;
    private int HIGH = 1;
    private int LOW = 0;
    
    public SimpleIntColorTransformFunction() {
        this(ColorLib.gray(0),ColorLib.gray(255));
    }
    
    public SimpleIntColorTransformFunction(Color start, Color end) {
        this(ColorLib.color(start),ColorLib.color(end));
    }
    
    public SimpleIntColorTransformFunction(int low, int high, Color start, Color end) {
        this(low,high,ColorLib.color(start),ColorLib.color(end));
    }
    public SimpleIntColorTransformFunction(int start, int end) {
        this(0,1,start,end);
    }
    
    public SimpleIntColorTransformFunction(int low, int high, int start, int end) {
        LOW = low;
        HIGH = high;
        START_COLOR = start;
        END_COLOR = end;
    }
    
    public void setStartColor(Color color) {
        setStartColor(ColorLib.color(color));
    }

    public void setStartColor(int color) {
        START_COLOR = color;
    }
    
        
    public void setEndColor(Color color) {
        setEndColor(ColorLib.color(color));
    }
    
    public void setEndColor(int color) {
        END_COLOR = color;
    }
    
    public void setLowValue(int low) {
        LOW = low;
    }
    
    public void setHighValue(int high) {
        HIGH = high;
    }
    
    public int getColor(int val) {
        float diff = (val - LOW)/(float)(HIGH - LOW);
        float[] shsb = getHSB(START_COLOR);
        float[] ehsb = getHSB(END_COLOR);
        float[] nhsb = new float[shsb.length];
        
        for (int i = 0; i < ehsb.length; i++) {
            nhsb[i] = (ehsb[i] - shsb[i])*diff + shsb[i];
        }
        int salpha = ColorLib.alpha(START_COLOR);
        int ealpha = ColorLib.alpha(END_COLOR);
        int alpha = (int)((ealpha - salpha)* diff + salpha);
        return ColorLib.setAlpha(Color.HSBtoRGB(nhsb[0],nhsb[1],nhsb[2]),alpha);        
    }
    
    public void weigh(int val) {
        if (val < LOW)
            LOW = val;
        if (val > HIGH) 
            HIGH = val;
    }
    
    public static float[] getHSB(int color) {
        return Color.RGBtoHSB(ColorLib.red(color), ColorLib.green(color), ColorLib.blue(color),null);
    }
}
